package cn.gson.crm.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.LongAccumulator;

import org.hibernate.engine.transaction.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.BackplanDAO;
import cn.gson.crm.model.dao.ConstactOrderDAO;
import cn.gson.crm.model.dao.CustomerinfoIDAO;
import cn.gson.crm.model.pojo.Backplan;
import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.Customerinfo;
import cn.gson.crm.model.vo.BackplanVO;
import cn.gson.crm.model.vo.ConstactOrderVO;

@Service
@Transactional
public class BackPlanService {
	@Autowired
	private BackplanDAO bDao;
	@Autowired
	private ConstactOrderDAO cDao;
	@Autowired
	private CustomerinfoIDAO cIdao;

	/**
	 * 获取所有回款计划
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<BackplanVO> getAllBackPlan(int page, int rows) {
		PageRequest pRequest = new PageRequest(page-1, rows);
		List<Backplan> listpo = bDao.getAllBackplan(pRequest);
		List<BackplanVO> listvo = new ArrayList<>();
		for (Backplan b : listpo) {
			listvo.add(returnBackplanVO(b));
		}
		return listvo;
	}
	
	/**
	 * 通过id获取回款计划
	 * @param id
	 * @return
	 */
	public BackplanVO getBackPlanById(Long id) {
		Backplan backplan = bDao.getBackPlanById(id);
		return returnBackplanVO(backplan);
	}

	public BackplanVO returnBackplanVO(Backplan b) {
		BackplanVO bVo = new BackplanVO(b.getBpId(), b.getBpCoId(), b.getBpCoName(), b.getBpCusId(), b.getBpCusName(),
				b.getBpIfBack(), b.getBpMoney(), b.getBpOwner(), b.getBpPeriod(), b.getBpRemarks(), b.getBpTime());
		ConstactOrder cOrder = b.getConstactOrder();
		if (cOrder != null) {
			ConstactOrderVO cVo = new ConstactOrderVO(cOrder.getCoId(), cOrder.getCoTheme());
			bVo.setBpOrderName(cVo.getCoTheme());
			bVo.setcOrderVO(cVo);
		}
		return bVo;
	}

	public Long count() {
		return bDao.count();
	}

	/**
	 * 添加计划回款
	 * @param backplan
	 */
	public void addSgf(Backplan backplan) {
		ConstactOrder cOrder = cDao.findOne(backplan.getBpCoId().longValue());
			backplan.setBpCoId(BigDecimal.valueOf(cOrder.getCoId()));
			backplan.setBpCoName(cOrder.getCoTheme());
			backplan.setConstactOrder(cOrder);
			bDao.save(backplan);
	}
	
	/**
	 * 编辑计划回款
	 * @param backplan
	 */
	public void editSgf(Backplan backplan,Long id) {
		ConstactOrder cOrder = cDao.findOne(backplan.getBpCoId().longValue());
		if(cOrder!=null) {
			backplan.setBpCoId(BigDecimal.valueOf(cOrder.getCoId()));
			backplan.setBpCoName(cOrder.getCoTheme());
			backplan.setConstactOrder(cOrder);
		}
		bDao.save(backplan);
	}
	
	public void deleteBackPlan(Long id) {
		try {
			bDao.delete(bDao.findOne(id));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
}
